
function buildContextMenu(options) {
	var anchor = options.anchor;
	var menu_items = options.menu_items;
	var add_items = options.add_items === undefined ? false : options.add_items;
	var header = options.header === undefined ? '' : options.header;
	var close_on_select = options.close_on_select === undefined ? false : options.close_on_select;

	console.log(anchor);

	if($("#simpleMenu").length > 0){
		$("#simpleMenu").remove();
	}

	var current_menu = undefined;
	var container = $('<div id="simpleMenu" class="simpleMenuContainer"></div>')
		.appendTo('body');
	first_crumb = $('<li class="breadcrumb">'+header+'</li>');
	breadcrumb = $('<ul id="simpleMenu_breadcrumb" class="breadcrumb_container"></ul>')
		.prependTo(container)
		.append(first_crumb);
	if(header === ''){
		breadcrumb.hide();
	}
	if(add_items){
		addItemsIcon(breadcrumb);
	}
	

	var def_menu = $('<ul id="simpleMenu_sub_All" class="simpleMenu_sub"></ul>')
		.appendTo(container);
	current_menu = def_menu;

	console.log(a = menu_items);

	for_each(menu_items, function(item) {
		buildMenuDOM(item, def_menu);
	});

	container.position({my: 'right top', at:'left bottom-10px', of: anchor});

	$(document).one("click", function() {container.remove(); });


	function buildMenuDOM(item, menu){
		if(item.hasOwnProperty('items')){
			$('<li><a href="#">'+item.name+' > </a> </li>')
			.prependTo(menu)
			.click(function(e){
				e.preventDefault();
				$(".simpleMenu_sub").hide();
				$("#simpleMenu_sub_"+item.name).show();
				if(breadcrumb.children("li").length === 1){
					breadcrumb.children("li").remove();
					$('<li class="breadcrumb"><a href="#">All</a><span class="ui-icon ui-icon-carat-1-e"></span></li>')
					.appendTo(breadcrumb)
					.children('a')
					.click(function(e){
						e.preventDefault();
						breadcrumbHandler(this);
						return false;
					});
				}
				$(breadcrumb).children('li').toggleClass('current_breadcrumb',false);
				$('<li class="breadcrumb current_breadcrumb"><a href="#">'+item.name+'</a><span class="ui-icon ui-icon-carat-1-e"></span></li>')
				.appendTo(breadcrumb)
				.children('a')
				.click(function(e){
					e.preventDefault();
					breadcrumbHandler(this);
					return false;
				});
				$(breadcrumb).show();
				return false;
			});
			var parent_menu = $('<ul class="simpleMenu_sub" id="simpleMenu_sub_'+item.name +'"></ul>')
				.appendTo(container).hide();
			for_each(item.items, function(item){
				buildMenuDOM(item, parent_menu);
			});
			
		}else{
			var img = '';
			if(item.checkbox){
				var img_url = item.selected ? "/images/Checked-checkbox-icon.png" : "/images/Unchecked-checkbox-icon.png";
				img = '<img src="'+img_url+'" height="13" width="13">';
			}
			var data = '';
			if(item.data !== undefined){
				for(var key in item.data){
					if(item.data.hasOwnProperty(key)){
						data = 'data-'+key + ' = "' + item.data[key] + '"';
					}
				}
			}
			$('<li '+data+'>'+img+'<label>'+item.name+'</label></li>')
			.appendTo(menu)
			.click(function(e){
				var anchor = anchor;
				if(item.events.click(this) && img !== ''){
					$(this).children("img").attr("src","/images/Checked-checkbox-icon.png");
				}
				else if(img !== ''){
					$(this).children("img").attr("src","/images/Unchecked-checkbox-icon.png");
				}
				container.position({my: 'right top', at:'left bottom-10px', of: anchor});
				return close_on_select;
			});
		}
	}

	function breadcrumbHandler(that){
		console.log(that);
		if($(that).text() === 'All'){
			breadcrumb.children("li").remove();
			breadcrumb.append(first_crumb);
			if(header === ''){
				breadcrumb.hide();
			}	
		}
		else{
			$(that).parent().nextAll().remove();
		}
		$(".simpleMenu_sub").hide();
		$("#simpleMenu_sub_"+$(that).text()).show();
		current_menu = $("#simpleMenu_sub_"+$(that).text());
	}

	function addItemsIcon(parent){
		$('<img src="/images/plus.png" class="breadcrumb_icon" height="20" width=20l>')
		.hover(function() {
			$(this).css('opacity', '1');
		}, function() {
			$(this).css('opacity', '0.3');
		})
		.click(function(event) {
			var img_url = "/images/Checked-checkbox-icon.png";
			$('<li><img src="'+img_url+'" height="13" width="13"> <input type="text" size="20"> </li>')
			.click(function(event) {
				var val = $(this).children('label').text();
				var idx = $.inArray(val, table.visible_cols);
				if (idx === -1) {
					table.visible_cols.push(val);
					$(this).children("img").attr("src","/images/Checked-checkbox-icon.png")
				} else {
					table.visible_cols.splice(idx, 1);
					$(this).children("img").attr("src","/images/Unchecked-checkbox-icon.png")
				}
				refreshTable();
				return false;
			})
			.insertAfter(current_menu.children().first()).children('input')
			.blur(function(event) {
				table.columns.push($(this).val());
				$('<label>'+$(this).val()+'</label>').replaceAll($(this))
				.parent().click();
				return false;
			}).keyup(function(event) {
				if(event.which == 13){
					$(this).blur();
				}
			})
			.focus();
			return false;
		})
		.appendTo(parent);
	}

	function addCheckbox(){

	}
}
